package com.shop.action;

import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionContext;

public class TestAction 
	implements 
	SessionAware,
	ServletRequestAware,
	ServletResponseAware,
	ServletContextAware,
	ApplicationAware {

	
	private Map<String, Object> session;
	private HttpServletRequest request;
	private HttpServletResponse response;
	private ServletContext application;
	private Map<String,Object> applicationMap;
	
	public String execute() {
		//HttpSession session = request.getSession();
		
		session.put("skey", "session value");
		request.setAttribute("rkey", "request value");
		application.setAttribute("akey", "servlet context value");
		applicationMap.put("appKey", "application value");
		
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		ServletContext ctx = ServletActionContext.getServletContext();
		
		Map<String,Object> sessionMap = ActionContext.getContext().getSession();
		Map<String,Object> appMap = ActionContext.getContext().getApplication();
		
		
		return "success";
	}
	
	
	public String ajax() throws Exception {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.print("hello,world");
		out.flush();
		out.close();
		return null;
	}
	

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public void setServletContext(ServletContext context) {
		this.application = context;
		
	}

	public void setApplication(Map<String, Object> application) {
		this.applicationMap = application;
	}
}
